package com.supcalls;

import java.util.Base64;

public class Base64Demo {
    public static void main(String[] args) {
        // 原始字符串
        String originalString = "Hello, Base64 Encoding and Decoding in Java 8!";

        // 编码
        String encodedString = Base64.getEncoder().encodeToString(originalString.getBytes());
        System.out.println("Encoded String: " + encodedString);

        // 解码
        byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
        String decodedString = new String(decodedBytes);
        System.out.println("Decoded String: " + decodedString);

        // URL和Filename安全编码
        String urlEncodedString = Base64.getUrlEncoder().encodeToString(originalString.getBytes());
        System.out.println("URL Encoded String: " + urlEncodedString);

        // URL和Filename安全解码
        byte[] urlDecodedBytes = Base64.getUrlDecoder().decode(urlEncodedString);
        String urlDecodedString = new String(urlDecodedBytes);
        System.out.println("URL Decoded String: " + urlDecodedString);

        // MIME 编码（适用于邮件或大文件）
        String mimeEncodedString = Base64.getMimeEncoder().encodeToString(originalString.getBytes());
        System.out.println("MIME Encoded String: " + mimeEncodedString);

        // MIME 解码
        byte[] mimeDecodedBytes = Base64.getMimeDecoder().decode(mimeEncodedString);
        String mimeDecodedString = new String(mimeDecodedBytes);
        System.out.println("MIME Decoded String: " + mimeDecodedString);
    }
}

